<template>
  <div class="login">
    <div class="card">
      <div class="img">
        <!-- <img src="../../../public/image/办公.png" alt="" /> -->
      </div>
      <div class="form">
        <h1>欢迎{{ substr }}</h1>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item v-show="isshow" label="昵称">
            <el-input v-model="form.nickname"></el-input>
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              v-model="form.password"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="submit(form)"
              type="primary"
              plain
              :disabled="isLog ? true : false"
              >{{ substr }}</el-button
            >
          </el-form-item>
          <el-form-item>
            {{ str
            }}<el-link type="primary" :underline="false" :href="subherf"
              >立即{{ sublink }}</el-link
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import "../../style/login/index.scss";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        nickname:''
      },
      timer: null,
      substr: "登录",
      sublink: "注册",
      str: "已有账号？",
      subherf: "/login?option=register",
      isshow: false,
      url:''
    };
  },
  computed: {
    isLog: function () {
      let condition = false;
      if (this.substr == "登录") {
        condition = this.form.username.length > 0 && this.form.password.length > 0;
      } else {
        condition = this.form.nickname.length > 0 && this.form.username.length > 0 && this.form.password.length > 0;
      }
      if (condition) return false;
      return true;
    },
  },
  created() {
    this.getroute();
  },
  methods: {
    getroute() {
      console.log(this.$route.query.option);
      if (this.$route.query.option == "register") {
        this.substr = "注册";
        this.sublink = "登录";
        this.str = "已有帐号？";
        this.subherf = "/login?option=login";
        this.isshow = true;
        this.form = {
          username: "",
          password: "",
          nickname: ""
        };
      } else {
        this.substr = "登录";
        this.sublink = "注册";
        this.str = "还没有账号？";
        this.subherf = "/login?option=register";
        this.isshow = false;
        this.form = {
          username: "",
          password: "",
          nickname:''
        };
      }
    },
    // 登录
    submit(e) {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        console.log(e);
        this.$axios.post(`/student/${this.$route.query.option || 'login'}`, e).then((res) => {
          if (res.data.code != this.$code)
            return this.$message.info(res.data.msg);
          this.$message.success(res.data.msg);
          if(this.$route.query.option == 'register'){
            this.$router.push("/login?option=login");
            this.getroute()
            }else{
            this.$router.push("/");
          }
          window.localStorage.setItem(
            "userInfo",
            JSON.stringify(res.data.data)
          );
          window.localStorage.setItem("token", res.data.token);
        });
      }, 500);
    },
  },
};
</script>